// pages/medical-record-delivery/payment.js
Page({
	data: {
		paymentAmount: '6000.00',
		passwordLength: 0,
		password: '',
		keypadRows: [
			['1', '2', '3'],
			['4', '5', '6'],
			['7', '8', '9'],
			['', '0', 'delete']
		]
	},

	onLoad(options) {
		console.log('支付页面加载')
		// 获取之前保存的表单数据
		const formData = wx.getStorageSync('medicalRecordFormData')
		if (formData && formData.totalAmount) {
			this.setData({
				paymentAmount: formData.totalAmount
			})
		}
	},

	// 键盘按键处理
	onKeyPress(e) {
		const key = e.currentTarget.dataset.key
		
		if (key === 'delete') {
			// 删除密码
			if (this.data.passwordLength > 0) {
				const newPassword = this.data.password.slice(0, -1)
				this.setData({
					password: newPassword,
					passwordLength: newPassword.length
				})
			}
		} else if (key !== '' && this.data.passwordLength < 6) {
			// 输入密码
			const newPassword = this.data.password + key
			this.setData({
				password: newPassword,
				passwordLength: newPassword.length
			})
			
			// 如果密码长度达到6位，自动提交
			if (newPassword.length === 6) {
				this.submitPayment(newPassword)
			}
		}
	},

	// 提交支付
	submitPayment(password) {
		wx.showLoading({
			title: '支付中...'
		})

		// 模拟支付请求
		setTimeout(() => {
			wx.hideLoading()
			
			// 模拟支付成功
			wx.showToast({
				title: '支付成功',
				icon: 'success',
				duration: 2000
			})

			// 跳转到支付成功页面
			setTimeout(() => {
				wx.redirectTo({
					url: '/pages/medical-record-delivery/payment-success'
				})
			}, 2000)
		}, 1500)
	}
}) 